package com.guoyh.ext

import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.guoyh.baseutils.ext.visible
import com.guoyh.baseutils.entity.LoadStatusEntity
import com.guoyh.baseutils.ext.getColorm
import com.guoyh.baseutils.ext.toast
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.guoyh.adapter.SimpleBannerAdapter
import com.guoyh.baserepos.BasePage
import com.guoyh.baseutils.entity.common.BaseBannerBean
import com.youth.banner.Banner
import com.youth.banner.indicator.RoundLinesIndicator
import com.youth.banner.util.BannerUtils

/**
 * 作者　: hegaojian
 * 时间　: 2020/11/5
 * 描述　:
 */


fun <T> Banner<*, *>.init(
    datas: MutableList<out BaseBannerBean<T>>,
    click: (T, Int) -> Unit,
    loopTime: Int = 3000
) {
    this.let {
        it.visible()
        val bannerAdapter = SimpleBannerAdapter(datas);
//        val bannerAdapter = DrllBannerAdapter(bannerData)
        it.apply {
            //多久轮播一次
            setLoopTime((loopTime).toLong())
            //设置圆角
            setBannerRound(0f)
            //适配器
            adapter = bannerAdapter
            //自动轮播
            isAutoLoop(true)
            //指示器样式
            indicator = RoundLinesIndicator(context)
            //指示器大小
            setIndicatorSelectedWidth(BannerUtils.dp2px(15f).toInt())
            //指示器 选中颜色
            setIndicatorSelectedColor(getColorm("#222222"))
            //指示器 未选中颜色
            setIndicatorNormalColor(getColorm("#999999"))
            //设置Banner 效果
//            setBannerGalleryEffect(18, 10)
        }

        bannerAdapter.setOnBannerListener { data, position ->
            click.invoke(data as T, position)
        }
    }
}


/**
 * 下拉刷新
 * @receiver SmartRefreshLayout
 * @param refreshAction Function0<Unit>
 * @return SmartRefreshLayout
 */
fun SmartRefreshLayout.refresh(refreshAction: () -> Unit = {}): SmartRefreshLayout {
    this.setOnRefreshListener {
        refreshAction.invoke()
    }
    return this
}

/**
 * 上拉加载
 * @receiver SmartRefreshLayout
 * @param loadMoreAction Function0<Unit>
 * @return SmartRefreshLayout
 */
fun SmartRefreshLayout.loadMore(loadMoreAction: () -> Unit = {}): SmartRefreshLayout {
    this.setOnLoadMoreListener {
        loadMoreAction.invoke()
    }
    return this
}

/**
 * 列表数据加载成功
 * @receiver BaseQuickAdapter<T,*>
 * @param baseListNetEntity BaseListNetEntity<T>
 */
fun <T> BaseQuickAdapter<T, *>.loadListSuccess(
    baseListNetEntity: BasePage<T>,
    smartRefreshLayout: SmartRefreshLayout
) {
    //关闭头部刷新
    if (baseListNetEntity.isRefresh()) {
        //如果是第一页 那么设置最新数据替换
        this.setNewInstance(baseListNetEntity.getPageData())
        smartRefreshLayout.finishRefresh()
    } else {
        //不是第一页，累加数据
        this.addData(baseListNetEntity.getPageData())
        //刷新一下分割线
        this.recyclerView.post { this.recyclerView.invalidateItemDecorations() }
    }
    //如果还有下一页数据 那么设置 smartRefreshLayout 还可以加载更多数据
    if (baseListNetEntity.hasMore()) {
        smartRefreshLayout.finishLoadMore()
        smartRefreshLayout.setNoMoreData(false)
    } else {
        //如果没有更多数据了，设置 smartRefreshLayout 加载完毕 没有更多数据
        smartRefreshLayout.finishLoadMoreWithNoMoreData()
    }
}

/**
 * 列表数据请求失败
 * @receiver BaseQuickAdapter<*, *>
 * @param loadStatus LoadStatusEntity
 * @param status LoadService<*>
 * @param smartRefreshLayout SmartRefreshLayout
 */
fun BaseQuickAdapter<*, *>.loadListError(
    loadStatus: LoadStatusEntity,
    smartRefreshLayout: SmartRefreshLayout
) {
    //关闭头部刷新
    if (loadStatus.isRefresh) {
        smartRefreshLayout.finishRefresh()
        //第一页，但是之前有数据，只给提示
        loadStatus.errorMessage.toast()
    } else {
        // 不是第一页请求，让recyclerview设置加载失败
        smartRefreshLayout.finishLoadMore(false)
        //给个错误提示
        loadStatus.errorMessage.toast()
    }
}


/**
 * 给adapter拓展的，防止重复点击item
 */
var adapterLastClickTime = 0L

fun BaseQuickAdapter<*, *>.setNbOnItemClickListener(
    interval: Long = 500,
    action: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Unit
) {
    setOnItemClickListener { adapter, view, position ->
        val currentTime = System.currentTimeMillis()
        if (adapterLastClickTime != 0L && (currentTime - adapterLastClickTime < interval)) {
            return@setOnItemClickListener
        }
        adapterLastClickTime = currentTime
        action(adapter, view, position)
    }
}

/**
 * 给adapter拓展的，防止重复点击item
 */
var adapterchildlastClickTime = 0L
fun BaseQuickAdapter<*, *>.setNbOnItemChildClickListener(
    interval: Long = 500,
    action: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Unit
) {
    setOnItemChildClickListener { adapter, view, position ->
        val currentTime = System.currentTimeMillis()
        if (adapterchildlastClickTime != 0L && (currentTime - adapterchildlastClickTime < interval)) {
            return@setOnItemChildClickListener
        }
        adapterchildlastClickTime = currentTime
        action(adapter, view, position)
    }
}